package com.crawleService.util.shanxitobaccoUtil;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;

public class SUtil {
    public static String encode2(String s) {
        String Base64Chars2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";
        if (s == null || s.length() == 0) return s;
        StringBuilder d = new StringBuilder();
        int[] b = ucs2_utf8(s);
        int b0, b1, b2, b3;
        int len = b.length;
        int i = 0;
        while (i < len) {
            int tmp = b[i++];
            b0 = (tmp & 0xFC) >> 2;
            b1 = (tmp & 0x03) << 4;
            if (i < len) {
                tmp = b[i++];
                b1 |= (tmp & 0xF0) >> 4;
                b2 = (tmp & 0x0F) << 2;
                if (i < len) {
                    tmp = b[i++];
                    b2 |= (tmp & 0xC0) >> 6;
                    b3 = tmp & 0x3F;
                } else {
                    b3 = 64; // 1 byte "-" is supplement
                }
            } else {
                b2 = b3 = 64; // 2 bytes "-" are supplement
            }
            d.append(Base64Chars2.charAt(b0));
            d.append(Base64Chars2.charAt(b1));
            d.append(Base64Chars2.charAt(b2));
            d.append(Base64Chars2.charAt(b3));
        }
        return d.toString();
    }

    public static int[] ucs2_utf8(String s) {
        if (s == null) return null;
        int[] d = new int[0];
        if (s.equals("")) return d;
        int c = 0, i = 0, j = 0;
        int len = s.length();
        int[] tempArray = new int[len * 3]; // Allocate a temporary array with maximum possible size
        while (i < len) {
            c = s.charAt(i++);
            if (c <= 0x7f) {
                // 1 byte
                tempArray[j++] = c;
            } else if ((c >= 0x80) && (c <= 0x7ff)) {
                // 2 bytes
                tempArray[j++] = ((c >> 6) & 0x1f) | 0xc0;
                tempArray[j++] = (c & 0x3f) | 0x80;
            } else {
                // 3 bytes
                tempArray[j++] = (c >> 12) | 0xe0;
                tempArray[j++] = ((c >> 6) & 0x3f) | 0x80;
                tempArray[j++] = (c & 0x3f) | 0x80;
            }
        }
        // Copy the actual contents to a new array of correct size
        d = Arrays.copyOf(tempArray, j);
        return d;
    }

    /**
     * 传入一个字符串， 返回一个加密之后的字符串
     *
     * @return
     */
    public static String encipher(String jsonData) {
        String urlEncodedData = "";
        try {
            urlEncodedData = encoding(jsonData).toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encode2(urlEncodedData);
    }

    public static StringBuilder encoding(String jsonData) throws UnsupportedEncodingException {
        StringBuilder encodedData = new StringBuilder();
        for (char c : jsonData.toCharArray()) {
            if (c == '@') {
                //   因为不会对象 @进行编码
                encodedData.append("@");
            } else if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                // 如果是中文字符，进行特定规则的修改
                String unicodeEncoded = String.format("%%u%04x", (int) c);
                unicodeEncoded = unicodeEncoded.substring(0, 2) + unicodeEncoded.substring(2).toUpperCase();
                encodedData.append(unicodeEncoded);
            } else {
                encodedData.append(URLEncoder.encode(String.valueOf(c), "UTF-8"));
            }
        }
        return encodedData;
    }
}
